Page({
  data: {
    semesterData: [],
    semesterSelected: '', //默认选中的学期
    keyword: '', //搜索关键字
    actTypesScource: [],
    actTypes: [{ text: '全部类型', value: '' }],
    actTypeSelected: '',//活动类型默认选中项
    pageIndex: 1,
    pageSize: 10,
    totalPages: 0,
    activities: [],
  },
  async searchCore(pageIndex) {
    let { statusCode, data: { code, data: { totalPages, actDatas } } } = await wx.$http.post("/quality/student/activityListForStu", {
      halfYear: this.data.semesterSelected,
      pageIndex: pageIndex,
      pageSize: this.data.pageSize,
      actTitle: this.data.keyword,
      actType: this.data.actTypeSelected
    }).catch();
    if (statusCode === 200 && code === 200) {
      let data = this.data.activities;
      if (pageIndex === 1)
        data.splice(0);
      actDatas.forEach(x => {
        x.actDesc = this.data.actTypesScource.find(q => q.actType == x.actType)?.actDesc;
        data.push(x);
      });

      this.setData({ activities: data, totalPages, pageIndex });
    }
  },
  async onSearch() {
    this.searchCore(1);
  },
  async onLoad(options) {
    let [actTypes, yearRes] = await wx.$http.all(
      wx.$http.post("/quality/actType/getDefaultActTypes"),
      wx.$http.post("/quality/actType/getActTypesByHalfYear")
    ).catch();
    let { statusCode, data: { data, code } } = actTypes;
    if (statusCode === 200 && code === 200) {
      let newActivityTypes = data.map(t => {
        return { text: t.actDesc, value: t.actType }
      });
      newActivityTypes.unshift({ text: "全部类型", value: '' });
      this.setData({
        actTypesScource: data,
        actTypes: newActivityTypes,
        actTypeSelected: ''
      })
    }

    ({ statusCode, data: { code, data } } = yearRes);
    if (statusCode === 200 && code === 200) {
      let years = data.map(x => { return { text: x.halfYearDesc, value: x.halfYear } });
      this.setData({ semesterData: years });
      if (years.length > 0)
        this.setData({ semesterSelected: years[0].value })
    }
    this.searchCore(1);
  },
  changeActType(e) {
    this.setData({ actTypeSelected: e.detail });
    this.searchCore(1);
  },
  changeSemester(e) {
    this.setData({ semesterSelected: e.detail });
    this.searchCore(1);
  },
  onReachBottom() {
    if (this.data.pageIndex < this.data.totalPages)
      this.searchCore(this.data.pageIndex + 1);
  },
  async onPullDownRefresh(){
    await this.searchCore(1);
    wx.stopPullDownRefresh();
  },
  onShareAppMessage(){
    return getApp().globalData.shareApp;
  }
})